<script setup lang="ts">
import { findRelGoods } from '@/service/api/goods'
import { IGoods } from '@/service/api/type/category.type';
import { Ref, ref } from 'vue'
const props = withDefaults(defineProps<{
  goodsId: string
}>(), {})

const sliders: Ref<IGoods[][]> = ref([])

const useRelGoodsData = async () => {
  const response = await findRelGoods(props.goodsId)
  const size = 4
  const total = Math.ceil(response.result.length / size)
  for (let i = 0; i < total; i++) {
    sliders.value.push(response.result.slice(i * size, (i + 1) * size))
  }
}
useRelGoodsData()
</script>

<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon" />
      <span class="title">{{ goodsId ? '同类商品推荐' : '猜你喜欢' }}</span>
    </div>
    <Carousel :sliders="sliders" style="height:380px" auto-play v-if="sliders.length" />
  </div>
</template>

<style lang="less" scoped>
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @themeColor;
      border-right: 4px solid @themeColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@themeColor, 40%);
      }
    }
  }
  :deep(.carousel) {
    height: 380px;
    .carousel {
      &-indicator {
        bottom: 30px;
        span {
          &.active {
            background: @themeColor;
          }
        }
      }
      &-btn {
        top: 110px;
        opacity: 1;
        background: rgba(0, 0, 0, 0);
        color: #ddd;
        i {
          font-size: 30px;
        }
      }
    }
  }
}
</style>
